package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.BaseStudentDao;
import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.studentdaofactory.StudentDaoFactory;

public class StudentService {
    //private OtherStudentDao studentDao = new OtherStudentDao();

    //用多态方法去解除耦合，利用StudentDaoFactory调用方法来实现
    private BaseStudentDao studentDao = StudentDaoFactory.getStudentDao();

    public boolean isExists(String id) {
        Student[] students = studentDao.findAllStudent();
        boolean exists = false;
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null && student.getId().equals(id)) {
                exists = true;
                break;
            }
        }
        return exists;
    }

    public boolean addStudent(Student student) {
        boolean flag = studentDao.addStudent(student);
        return flag;
    }

    public Student[] findAllStudent() {
        //调用底层Dao方法，拿到数组
        Student[] students = studentDao.findAllStudent();
        boolean flag = false;
        //循环判断数组是否为空
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null) {
                flag = true;
            }
        }
        //根据flag值，确定返回数组
        if (flag) {
            return students;
        } else {
            return null;
        }
    }

    public void deleteStudentInfo(String deleteId) {
        studentDao.deleteStudentInfo(deleteId);
    }

    public void updataStudentInfo(Student student, String updataId) {
        studentDao.updataStudentInfo(student, updataId);
    }
}
